
// 环境判断
export function isWeChatApplet() {
  return new Promise((resolve) => {
    wx.miniProgram.getEnv(res => {
      if (res.miniprogram) { // 在小程序中
        resolve(true)
      } else {
        resolve(false)
      }
    })
    resolve(false)
  })
}

// 获取url中参数
export function getURLParamByKey(paramKey) {
  if (!paramKey) {
    throw Error('参数不能为空')
  }
  const searchParams = new URLSearchParams(window.location.search)
  const res = searchParams.get(paramKey)
  return res ?? ''
}


// 对象转数组
export function objectToArray(object) {
  const arr = []
  Object.keys(object).forEach((key) => {
    arr.push({ name: object[key], text: object[key], value: key })
  })
  return arr
}

// 脱敏
export function encryptString(string, beginIndex, length) {
  const arr = string?.split('')
  let substrLength = 0
  let str = ''
  arr?.map((item, index) => {
    if (index >= beginIndex && substrLength < length) {
      substrLength++
      item = '*'
    }
    str += item
  })
  return str
}

// 将富文本的图片转成图片链接
export const getImgSrc = (richtext) => {
  let imgList = []
  richtext.replace(/<img [^>]*src=['"]([^'"]+)[^>]*>/g, (match, capture) => {
    imgList.push(capture)
  })
  return imgList
}
